var Ad = {};
Ad.edit = {
    init : function() {
        $('[name=type]').click(function(e) {
            var type  = $(this).val(),
                nodes = [];

            switch (type) {
                case 'zl' : 
                    nodes = [
                        ['cpm_size',  0],
                        ['size',      0],
                        ['html_code', 0],
                        ['img_url',   0],
                        ['zl_item',   1]
                    ];
                    break;

                case 'tw' : 
                    nodes = [
                        ['cpm_size',  0],
                        ['size',      1],
                        ['html_code', 1],
                        ['img_url',   0],
                        ['zl_item',   0]
                    ];
                    break;

                case 'img' :
                    nodes = [
                        ['cpm_size',  0],
                        ['size',      1],
                        ['html_code', 0],
                        ['img_url',   1],
                        ['zl_item',   0]
                    ];
                    break;

                case 'zn' :
                    nodes = [
                        ['cpm_size',  0],
                        ['size',      0],
                        ['html_code', 1],
                        ['img_url',   0],
                        ['zl_item',   0]
                    ];
                    break;
            }

            $.each(nodes, function(k, v) {
                $('[g=' + v[0] + ']')[(v[1] ? 'show' : 'hide')]();
            })
        }).trigger('click');
    }
};

/**
 * 充值
 */
var Recharge = {
    win : {},   //窗口对象

    add : function(userName, userType, dao) {
        dao = dao || {};

        if (!dao.user_name) {
            dao.user_name = userName;
        }

        if (!dao.user_type) {
            dao.user_type = userType;
        }

        this.openAddDialog(dao);
    },

    openAddDialog : function(dao) {
        var $h = $($('#t-recharge-form').html()),
            _this = this;

        this.win = dialog({
                title   : '手工充值',
                content : $h,
                width   : 400,
                height  : 180,
                okValue : '保存',
                ok      : function () {
                    var d = _this.getFormDao($h);
                    _this.save(d);

                    return false;
                },
                cancelValue: '取消',
                cancel: function () {}
            });

        this.win.showModal();
        dao.user_type = parseInt(dao.user_type, 10);

        switch (dao.user_type) {
            case 1 :
                $h.find('[g=money_pre]').hide();
                break;

            case 2 : 
                $h.find('[g=money_type]').hide();
                break;

            default :
                alert('会员类型有误');

                return false;
        }

        this.fillForm($h, dao);
    },
    
    getFormDao : function($f) {
        var dao = {
            id         : $f.find('[name=id]').val(),
            user_name  : $f.find('[name=user_name]').val(),
            money_type : $f.find('[name=money_type]:checked').val(),
            type       : $f.find('[name=type]:checked').val(),
            money      : $f.find('[name=money]').val(),
            comment    : $f.find('[name=comment]').val()
        };

        return dao;
    },

    fillForm : function($f, dao) {
        if (dao.user_name) {
            $f.find('[name=user_name]').val(dao.user_name);
        }

        dao.money_type = dao.money_type || 1;
        $f.find('[name=money_type][value=' + dao.money_type + ']').attr({ checked : true});

        if (dao.money) {
            $f.find('[name=money]').val(dao.money);
        }

        if (dao.comment) {
            $f.find('[name=comment]').val(dao.comment);
        }

        if (dao.type) {
            $f.find('[name=type][value='+dao.type+']').attr({checked : true});
        }

        /**
         * 只有扣除预充值时才会有id
         */
        if (dao.id) {
            $f.find('[name=id]').val(dao.id);
            var disabled = {disabled : true};
            $f.find('input[type=text]').attr(disabled);
            $f.find('input[type=radio]').attr(disabled);
            $f.find('textarea').attr(disabled);
        }

        return true;
    },

    save : function(dao) {
        var url   = '?c=Recharge&a=',
            _this = this;

        url += (dao.id) ? 'doDeduct' : 'doSave';

        $.post(url, dao, function(j) {
            if (!j.s) {
                alert(j.m);

                return false;
            }

            window.location.href = window.location.href;
            _this.win.close();
        }, 'json');
    },

    deduct : function(id) {
        if (typeof(id) == 'undefined') {
            alert('参数不完整');
        }

        var _this = this,
            d = dialog({
            content : '正在载入数据，请稍等...'
        });
        d.showModal();

        $.getJSON('?c=Recharge&a=getById', {id : id}, function(j) {
            d.close();

            if (!j.s) {
                alert(j.m);

                return false;
            }

            var dao = j.m;

            dao.type       = 2;
            dao.money_type = 1;
            dao.user_type  = 2;
            dao.comment    = '扣除' + dao.finish_time + '预充的' + dao.money + '元';

            _this.openAddDialog(dao);
        })
    }

};
